<template>
  <div class="components-container">
 <!--    <div class="info">UE编辑器示例<br>需要使用编辑器时，调用UE公共组件即可。可设置填充内容defaultMsg，配置信息config(宽度和高度等)，可调用组件中获取内容的方法。支持页面内多次调用。</div> -->
    <div class="editor-container">
      <input type="text" class="title" placeholder="文章标题" v-model="title">
      <UE :defaultMsg=defaultMsg :config=config :id=ue1 ref="ue"></UE>
      <button @click="submit()">发布文章</button>
    </div>
  </div>
</template>

<script>
  import UE from 'COMPS/ue.vue';
  export default {
    components: {UE},
    data() {
      return {
        defaultMsg: '',
        title: '',
        config: {
          initialFrameWidth: null,
          initialFrameHeight: 350
        },
        ue1: "ue1", // 不同编辑器必须不同的id
      }
    },
    mounted() {
      this._getArticleTitle();
    },
    methods: {
      _getArticleTitle() {
        let that = this;
        this.$refs.ue.getArticleHtml().then(res => {
          that.title = res;
        })
      },
      submit() { 
        let contentHtml = this.$refs.ue.getUEContent(); // 获取内容
        let contentText = this.$refs.ue.getUEContentTxt(); // 获取纯文本内容

        if(this.title == "") {
          return alert("文章标题不能为空")
        }
        if (contentText.length == 0) {
          return alert("文章内容不能为空")
        }
        let that = this;
        this.$axios.post('/news/publish', {
          title: that.title,
          content: contentHtml
        }).then((res) => {
          if (res.status == 200) {
            alert('发布成功')
          } else {
            alert(res.msg)
          }
        })
      }
    }
  };
</script>

<style lang="less">
  .info{
    border-radius: 10px;
    line-height: 20px;
    padding: 10px;
    margin: 10px;
    background-color: #ffffff;
  }
  .components-container {
    box-sizing: border-box;
    .editor-container {
      width: 800px;
      margin: 0 auto;
      >button {
        cursor: pointer;
        border: none;
        height: 30px;
        margin-top: 10px;
        padding: 0 10px;
        color: #fff;
        border-radius: 2px;
        background-color: #5ca8e5;
        &:hover {
          background-color: #1582e9;
        }
      }
      .title {
        box-sizing: border-box;
        width: 100%;
        padding-left: 10px;
        height: 36px;
        font-size: 18px;
        outline: none;
        color: #999;
        border: 1px solid rgb(204, 204, 204);
      }
    }
  }
</style>



